<?php
/**
 * @filename QuestionSubjectClassify.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2014-12-11 11:21:20
 * @version 1.0
 * @Description 问卷归类
 */
class QuestionSubjectClassify extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'question_subject_classify';
    }

	//级别，有些没有支行，直接到网点
    const QUESTION_NORM_ONE = 1;  //指标层级：一级
    const QUESTION_NORM_TWO = 2;  //指标层级：二级
    const QUESTION_NORM_THREE = 3;  //指标层级：三级
	
    /*
     * 获取项目所有状态
     */
    public static function questionNorm(){
            return array(
                    self::QUESTION_NORM_ONE => array(
                            'key' => 'QUESTION_NORM_ONE',
                            'desc' => '一级指标'
                    ),
                    self::QUESTION_NORM_TWO => array(
                            'key' => 'QUESTION_NORM_TWO',
                            'desc' => '二级指标'
                    ),
                    self::QUESTION_NORM_THREE => array(
                            'key' => 'QUESTION_NORM_THREE',
                            'desc' => '三级指标'
                    )
            );
    }
	
	
    /**
	 * 得到归类键值对
	 * @param type $question_id
	 * @param type $classify_type  归类题目类型
	 * @return type
	 */
    public function getKv($question_id, $classify_type=0){
		$query = 'status = 1';
		if ($question_id) {
			$query .= " and question_id=".$question_id;
		}
		if ($classify_type) {
			$query .= " and classify_type=".$classify_type;
		}
        $data = $this->findAll($query);
        $result = array();
        if ($data) {
            foreach ($data as $key => $val) {
                $classify_id = $val['classify_id'];
                $classify_name = $val['classify_name'];
                $result[$classify_id] = $classify_name;
            }
            return $result;
        }
        return $data;
    }
	
	/**
	 * 根据 三级指标classify_id 获取此归类的详细归类数据
	 * @param type $question_id
	 * @param type $classify_type  归类题目类型
	 * @return type
	 */
    public function getClassifyByThree($classify_id, &$result = array()){
		if (!$classify_id) {
			return false;
		}
		
		# 规定显示的参数
		$types = array(
			self::QUESTION_NORM_ONE => 'one',
			self::QUESTION_NORM_TWO => 'two',
			self::QUESTION_NORM_THREE => 'three',
		);
		if (empty($result)) {
			foreach ($types as $key => $val) {
				$result[$val] = array();
			}
		}
		
		$query = 'status = 1';
		if ($classify_id) {
			$query .= " and classify_id=".$classify_id;
		}
        $data = $this->findAll($query);
		
        if ($data) {
            foreach ($data as $key => $val) {
                $classify_id = $val['classify_id'];
                $classify_name = $val['classify_name'];
				$classifyKey = $types[$val['classify_type']];
				if ($val['parent_id']) {
					$result[$classifyKey][$classify_id] = $val;
					$val['parent'] = $this->getClassifyByThree($val['parent_id'], $result);
				}
				unset($val['parent']);
				$result[$classifyKey][$classify_id] = $val;
				
            }
        }
		
        return $result;
    }
	
    /**
     * 得到问卷的各级分类
     */
    public function getByQuestion($question_id){
        $data = $this->findAll('question_id='.$question_id);
        $result = array();
        if ($data) {
            foreach ($data as $key => $val) {
                $classify_type = $val['classify_type'];
                $classify_id = $val['classify_id'];
                $parent_id = $val['parent_id'];
                $result['list'][$classify_type][] = $classify_id;
                $result['parent'][$parent_id][] = $classify_id;
                $classify_name = str_replace(array('一级指标','二级指标','三级指标'), '', $val['classify_name']);
                $classify_name = str_replace(array(':','：'), '', $classify_name);
                $result['all'][$classify_id] = $classify_name;
                if ($classify_type == 3) {//查看这个级别的题型是否有选择题，用于显示选项，选项值这两列
                    $result[$classify_id]['option'] = QuestionSubject::model()->count('classify_id='.$classify_id.' and question_type=2 and status=1');
                }
            }
            if (!isset($result['list'][1])) {//没有一级
                $result['list'][1] = array(0);
            }
            if (!isset($result['list'][2])) {//没有二级
                $result['list'][2] = array(0);
            }
            //print_r($result);exit;
            return $result;
        }
        return $data;
    }
}